package com.corticon.dialogs.validation.designer;

/**
 * server validator in create/update question
 */
import com.corticon.dialogs.model.Questionnaire;
import com.corticon.dialogs.validation.GenericHibernateValidator;
import com.corticon.dialogs.validation.ValidationMessageFormatter;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.Errors;

public class QuestionnaireValidator extends GenericHibernateValidator<Questionnaire> {

    public QuestionnaireValidator(
            ValidationMessageFormatter validationMessageFormatter) {
        super.validationMessageFormatter = validationMessageFormatter;
    }

    @Override
    protected void complexValidate(Object object, Errors errors) {
        super.complexValidate(object, errors);

        Questionnaire validator = (Questionnaire) object;

        List<Character> vocabularyExcludedChars = new ArrayList<Character>();
        vocabularyExcludedChars.add('_');

        String[] nameArgs = {"Name"};
        commonCharactersValidate(validator.getName(), vocabularyExcludedChars,
                errors, "name", "questionBase.validate.name.invalidChars",
                nameArgs, "default message");
        String[] nameArgs1 = {"Vacabulary name"};
        commonCharactersValidate(validator.getVocabularyname(), vocabularyExcludedChars,
                errors, "vocabularyname", "questionBase.validate.vacabularyname.invalidChars",
                nameArgs1, "default message");

        if (StringUtils.isEmpty(validator.getDisplayTitle())) {
            errors.rejectValue("displayTitle", "questionnaire.validate.displaytitle.required",
                    "default message");
        }
        if (StringUtils.isEmpty(validator.getFunction().getCode())) {
            errors.rejectValue("function", "questionnaire.validate.function.required",
                    "default message");
        }

        if (StringUtils.isEmpty(validator.getRegion().getCode())) {
            errors.rejectValue("region", "questionnaire.validate.region.required",
                    "default message");
        }

        if (StringUtils.isEmpty(validator.getProductType().getCode())) {
            errors.rejectValue("productType", "questionnaire.validate.productType.required",
                    "default message");
        }

        if (StringUtils.isEmpty(validator.getLocale().getCode())) {
            errors.rejectValue("locale", "questionnaire.validate.locale.required",
                    "default message");
        }

        if (StringUtils.isEmpty(validator.getTheme().getCode())) {
            errors.rejectValue("theme", "questionnaire.validate.theme.required",
                    "default message");
        }
    }
}
